package com.woniuxy.dr_mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;
import com.woniuxy.dr_mall.entity.DrFavoriteView;
import com.woniuxy.dr_mall.entity.DrPicture;
import com.woniuxy.dr_mall.entity.DrUser;
import com.woniuxy.dr_mall.entity.MyResponseEntity;
import com.woniuxy.dr_mall.global.Global;
import com.woniuxy.dr_mall.global.ResponseStateCode;
import com.woniuxy.dr_mall.mapper.DrUserMapper;
import com.woniuxy.dr_mall.service.IDrUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@Service
public class DrUserServiceImpl extends ServiceImpl<DrUserMapper, DrUser> implements IDrUserService {
    @Autowired
    private DrUserMapper userMapper;

    @Autowired
    private JavaMailSender mailSender;

    private Map<String,String> validateInfo = new HashMap<>();

    @Override
    public MyResponseEntity<String> register(String account, String password,String vCode) {
        if (userMapper.getByAccount(account) != null) {
            //账号重复
            return new MyResponseEntity<String>(ResponseStateCode.ACCOUNT_MULTIPLE,"账号重复");
        }

        if(validateInfo.get(account)==null ||!validateInfo.get(account).equals(vCode)){
            return new MyResponseEntity<String>(ResponseStateCode.VALIDATE_ERROR,"验证码错误");
        }
        DrUser user = new DrUser();
        user.setAccount(account);
        user.setPassword(password);
        user.setStatus(Global.UNNORMAL);
        userMapper.insert(user);
        validateInfo.remove(account);
        return MyResponseEntity.SUCCESS;
    }

    @Override
    public DrUser login(String account, String password) {
        return userMapper.getByActAndPwd(account,password);
    }

    @Override
    public void editPasswordById(Integer id, String password) {
        userMapper.editPasswordById(id,password);
    }

    @Override
    public MyResponseEntity<String> getValidateCode(String email) {
        if (userMapper.getByAccount(email) != null) {
            return new MyResponseEntity<String>(ResponseStateCode.ACCOUNT_MULTIPLE,"账号重复");
        }
        //生成一个验证码并发送邮件
        String code = new Random().nextInt(9000)+1000+"";
        validateInfo.put(email,code);
        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人
        message.setFrom("z13550344740@163.com");
        // 收件人
        message.setTo(email);
        // 邮件标题
        message.setSubject("DR商城注册验证码");
        // 邮件内容
        message.setText("您的验证码是"+code);
        mailSender.send(message);
        return MyResponseEntity.SUCCESS;
    }

    @Override
    public MyResponseEntity<String> validEmail(String email) {
        if (userMapper.getByAccount(email) != null) {
            return new MyResponseEntity<String>(ResponseStateCode.ACCOUNT_MULTIPLE,"账号重复");
        }
        return MyResponseEntity.SUCCESS;
    }

    //使用UserId来查询用户的信息
    @Override
    public DrUser selectImformationById(int userId) {
        DrUser drUser = userMapper.selectImformationById(userId);
        if(drUser.getBirthday()!=null){
            String[] s = drUser.getBirthday().split("-");
            drUser.setYear(s[0]);
            drUser.setMonth(s[1]);
            drUser.setDay(s[2]);
            return drUser;
        }
        else{
            drUser.setYear(" ");
            drUser.setMonth(" ");
            drUser.setDay(" ");
            return drUser;
        }
    }

    //通过用户id修改用户的信息 （叶官鑫 2021-8-14）
    @Override
    public void useIdUpdateImformation(DrUser user) {
        String birthday = user.getYear()+"-"+user.getMonth()+"-"+user.getDay();
        user.setBirthday(birthday);
        userMapper.useIdUpdateImformation(user);
    }

    //使用用户id查询用户的收藏信息 叶官鑫 2021-8-16
    @Override
    public List<DrFavoriteView> selectUserFavoriteById(int userId) {
        List<DrFavoriteView> userFavorites = userMapper.useUserIdSelectImformation(userId);
        return userFavorites;
    }

    @Override
    public List<DrUser> getUsers() {
        return userMapper.getUsers();
    }

    @Override
    public void getNormal(int id) {
        userMapper.getNormal(id);
    }

    @Override
    public void getUnNormal(int id) {
        userMapper.getUnNormal(id);
    }

    @Override
    public DrUser getByAccount(String account) {
        return userMapper.getByAccount(account);
    }

    @Override
    public DrUser getByAccountAndPassword(String account, String password) {
        return userMapper.getByActAndPwd(account,password);
    }

    @Override
    public DrUser getById(int id) {
        return userMapper.getById(id);
    }
}
